package com.itheima.security.distributed.gateway.common;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author: yangguoxiong
 * @date: 2022/3/8 16:08
 * @description:
 */
public class JwtUtil {

    public static String BEARER = "bearer";
    public static Integer AUTH_LENGTH = 7;
    public static String SIGNING_KEY = "uaa123";

    public static String getToken(String auth) {
        if (auth != null && auth.length() > AUTH_LENGTH) {
            String headStr = auth.substring(0, 6).toLowerCase();
            if (headStr.compareTo(BEARER) == 0) {
                auth = auth.substring(7);
            }
            return auth;
        } else {
            return null;
        }
    }

    public static Claims parseJWT(String jsonWebToken) {
        try {
            return (Claims) Jwts.parser().setSigningKey(Base64.getDecoder().decode(getBase64Security())).parseClaimsJws(jsonWebToken).getBody();
        } catch (Exception var2) {
            return null;
        }
    }

    public static String getBase64Security() {
        return Base64.getEncoder().encodeToString(SIGNING_KEY.getBytes(StandardCharsets.UTF_8));
    }

}
